package com.xhwl.centiotdata.handler.visitor.impl;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.YaoAnVRRecordDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IPlatformGovernmentService;
import com.xhwl.centiotdata.service.cent.IVisitorCentService;
import com.xhwl.centiotdata.suppliersystem.yaoan.VisitorYaoAnHandler;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.pojo.cent.government.EmployeeVRExperience;
import com.xhwl.common.pojo.cent.government.EmployeeVRExperienceDetail;
import com.xhwl.common.pojo.cent.visitor.yaoan.VisitorVRExperience;
import com.xhwl.common.pojo.cent.visitor.yaoan.VisitorVRExperienceDetail;
import com.xhwl.common.query.cent.device.ProductAccessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UnknownFormatConversionException;
import java.util.stream.Collectors;

/**
 * @author tangwei
 * @description 蒙牛访客VR对接耀安VR体验馆
 * @create 2022-11-14 13:40
 */
@Slf4j
@Service
public class VisitorIntegrationYaoAnHandler extends VisitorIntegrationHandler {

    @Autowired
    private VisitorYaoAnHandler visitorYaoAnHandler;
    @Autowired
    private IVisitorCentService visitorCentService;
    @Autowired
    private IDeviceCentService iDeviceCentService;
    @Autowired
    private IPlatformGovernmentService platformGovernmentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        VisitorIntegrationFactory.register(ProductEnum.YAOAN_VR.getId(), this);
    }
    /**
     * 推送人员数据给耀安VR系统
     * @param configDTO
     * @param authDTO
     * @return
     */
    @Override
    public ResultJson createVrVisitor(VisitorIntegrationConfigDTO configDTO, VisitorAuthDTO authDTO) throws Exception {
        visitorYaoAnHandler.createUser(configDTO,authDTO);
        return ResultJson.success();
    }

    /**
     * 获取VR体验记录及详情
     * @param configDTO
     * @return
     */
    @Override
    public void getVisitorVrRecord(VisitorIntegrationConfigDTO configDTO) throws Exception {
        // 获取访客集成配置
        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setProjectId(configDTO.getProjectId());
        productAccessQuery.setCategoryId(CategoryEnum.VISITOR_SYSTEM.getId());
        List<DeviceProjSupplierCategory> deviceConfigList = iDeviceCentService.listProductAccess(null, productAccessQuery).stream()
                .filter(item -> Objects.equals(item.getProductId(),ProductEnum.YAOAN_VR.getId())).collect(Collectors.toList());
        JSONObject properties = deviceConfigList.get(0).getProperties();
        log.info("获取的访客VR集成配置{}",properties);
        // 拉取VR体验记录
        List<YaoAnVRRecordDTO> result = visitorYaoAnHandler.getRecord(configDTO,properties.getString("deptCode"));
        List<VisitorVRExperience> visitorVRList = new ArrayList<>();
        List<EmployeeVRExperience> employeeVRList = new ArrayList<>();
        result.forEach(item -> {
            if (properties.getString("deptCode").equals(item.getDeptCode())){
                VisitorVRExperience dto = new VisitorVRExperience();
                dto.setName(item.getName());
                dto.setIdCard(item.getIdCard());
                dto.setPassTime(item.getPassTime());
                dto.setExperienceNum(item.getNum());
                dto.setDeptCode(item.getDeptCode());
                visitorVRList.add(dto);
            } else {
                EmployeeVRExperience dto = new EmployeeVRExperience();
                dto.setAccountId(item.getLsLaborsKey());
                dto.setName(item.getName());
                dto.setIdCard(item.getIdCard());
                dto.setPassTime(item.getPassTime());
                dto.setExperienceNum(item.getNum());
                dto.setDeptCode(item.getDeptCode());
                employeeVRList.add(dto);
            }
        });
        // 存储体验记录到业务中台
        visitorCentService.saveVisitorVrRecord(visitorVRList);
        platformGovernmentService.saveEmployeeVrRecord(employeeVRList,properties.getInteger("enterpriseId"));
        // 处理体验详情数据
        List<String> visitorIdCards = visitorVRList.stream().map(VisitorVRExperience::getIdCard).distinct().collect(Collectors.toList());
        List<String> employeeIdCards = employeeVRList.stream().map(EmployeeVRExperience::getIdCard).distinct().collect(Collectors.toList());
        List<VisitorVRExperienceDetail> visitorDetailList = new ArrayList<>();
        List<EmployeeVRExperienceDetail> employeeDetailList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(visitorIdCards)){
            visitorIdCards.forEach(idCard -> {
                try {
                    List<VisitorVRExperienceDetail> visitorDetail =  visitorYaoAnHandler.getVisitorDetailList(configDTO,idCard);
                    visitorDetailList.addAll(visitorDetail);
                }catch (Exception e){
                    throw new BusinessException("获取访客VR体验记录详情异常{}",e);
                }
            });
        }
        if (CollectionUtils.isNotEmpty(employeeIdCards)){
            employeeIdCards.forEach(idCard -> {
                try {
                    List<EmployeeVRExperienceDetail> employeeDetail =  visitorYaoAnHandler.getEmployeeDetailList(configDTO,idCard,properties.getString("deptCode"));
                    employeeDetailList.addAll(employeeDetail);
                } catch (Exception e) {
                    throw new BusinessException("获取员工VR体验记录详情异常{}",e);
                }
            });
        }
        // 存储体验记录详情到业务中台
        visitorCentService.saveVisitorVrDetail(visitorDetailList);
        platformGovernmentService.saveEmployeeVrDetail(employeeDetailList);
    }
}
